#pragma once
#include "component.h"
#include <string>
#include <ifaddrs.h>
#include <arpa/inet.h>

class Socket {
  public:
    DISALLOW_COPY_AND_MOVE(Socket);
    Socket();
    ~Socket();
    void set_fd(int fd);
    int fd() const;
    std::string get_addr() const;
    RC create();
    RC bind(const char *ip, uint16_t port) const;
    RC listen() const;
    RC accept(int &clnt_fd) const;
    RC connect(const char *ip, uint16_t port) const;
    RC SetNonBlocking() const;
    bool IsNonBlocking() const;
    size_t RecvBufSize() const;
    static const char *getLocalIP(const char *interface);
    static uint8_t getLocalIPLastOctet(const char *interface);

  private:
    int fd_;
};
